var qcloud = require('../../../vendor/sdk/index');
var config = require('../../../config');
var app = getApp();

Page({
  data: {
    navH: app.globalData.navHeight,
    adminUserList: [],
    fileHost: app.globalData.imageUrl,
    addNormalAdminUserModal: false,
    chooseArr: [],
    noDataTip: '......'
  },
  onLoad() {
    this.getAdminUserList();
  },
  onNavBack() {
    wx.navigateBack();
  },
  getAdminUserList() {
    let self = this;
    qcloud.request({
      url: config.service.getUserListUrl,
      data: { role: 9 },
      success: function(res) {
        console.log('adminuserList res', res);
        if (res.data.success == true) {
          self.setData({ adminUserList: res.data.data });
        }
      }
    });
  },
  keywordsChange(e) {
    this.setData({ keyWords: e.detail.value });
  },
  search() {
    this.setData({
      chooseArr: [],
      userList: []
    });
    if (!this.data.keyWords) {
      return;
    }
    let self = this;
    // 搜索符合关键词的role=2用户数据
    qcloud.request({
      url: config.service.getUserListUrl,
      data: { name: self.data.keyWords, role: 2 },
      success: function(res) {
        console.log('userList res', res);
        if (res.data.success == true) {
          let userList = res.data.data;
          if (userList && userList.length > 0) {
            userList.forEach(item => {
              item.disabled = false;
            });
            self.setData({ userList });
          } else {
            self.setData({
              noDataTip: `未查询到与"${self.data.keyWords}"相匹配数据`
            });
          }
        }
      }
    });
  },
  showAddNormalAdminUserModal() {
    this.setData({
      addNormalAdminUserModal: true
    });
  },
  clear() {
    this.setData({
      userList: [],
      searchValue: null,
      addNormalAdminUserModal: false
    });
  },
  modalCancel() {
    console.log('modalCancel');
  },
  modalConfirm() {
    console.log('modalConfirm');
    if (this.data.chooseArr && this.data.chooseArr.length === 0) {
      wx.showToast({
        title: '请至少选择一位用户',
        icon: 'none',
        image: '',
        duration: 3000,
        mask: false
      });
      return;
    }
    let self = this;
    // 批量添加普通管理员
    qcloud.request({
      url: config.service.batchUpdateUserRoleUrl,
      data: { ids: self.data.chooseArr.join(',') },
      success: res => {
        if (res.data.success) {
          wx.showToast({
            title: '添加成功',
            icon: 'none',
            image: '',
            duration: 3000,
            mask: false
          });
          // 查询最新普通管理员列表
          self.getAdminUserList();
          self.clear();
        }
      }
    });
  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);
    let chooseArr = e.detail.value;
    let userList = this.data.userList;
    let self = this;
    if (chooseArr && chooseArr.length == 9 - this.data.adminUserList.length) {
      // 标记除选中之外的其他的选项为disabled
      userList &&
        userList.forEach(item => {
          if (chooseArr.indexOf(item.id.toString()) === -1) {
            item.disabled = true;
          }
        });
    } else {
      userList &&
        userList.forEach(item => {
          item.disabled = false;
        });
    }
    this.setData({ chooseArr: chooseArr, userList: userList });
  },
  delete(e) {
    let self = this;
    let item = e.target.dataset.item;
    console.log('eee', item);

    wx.showModal({
      title: '提示',
      content: `确定从普通管理员列表中移除 ${item.name}?`,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          qcloud.request({
            url: config.service.updateUserInfoUrl,
            data: { id: item.id, role: 2 },
            success: res => {
              console.log('update-response', res);
              if (res.data.success) {
                wx.showToast({
                  title: '成功从普通管理员列表中移除!',
                  icon: 'none',
                  image: '',
                  duration: 1500,
                  mask: false,
                  complete: () => {
                    // 将移除的用户从data状态中干掉
                    self.setData({
                      adminUserList: self.data.adminUserList.filter(
                        it => it.id !== item.id
                      )
                    });
                  }
                });
              }
            }
          });
        }
      }
    });
    return;
  }
});
